package apis

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"net/http"
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
	"tuowei.com/pkg/util"
	"tuowei.com/pkg/xlog"
)

type SysStudent struct {
	api.Api
}

func (e SysStudent) GetPage(c *gin.Context) {
	s := service.SysStudent{}
	req := dto.SysStudentGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]models.SysStudent, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}
	//为了升本年份
	for i, j := range list {
		list[i].ShenBenYear = j.ShenBenTime.Format("2006")
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e SysStudent) Insert(c *gin.Context) {
	req := dto.SysStudentInsertReq{}
	s := service.SysStudent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	lastID, err := s.Insert(&req)
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(lastID, "成功")
	return
}

func (e SysStudent) Get(c *gin.Context) {
	s := service.SysStudent{}
	orderService := service.SysOrder{}
	req := dto.SysStudentById{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		MakeService(&orderService.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.SysStudent
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(http.StatusUnprocessableEntity, err, "查询失败")
		return
	}
	offlineLists, onlineLists, materialLists := orderService.GetBuyRecordByStudentId(object.Id)
	object.OfflineOrders = offlineLists
	object.OnlineOrders = onlineLists
	object.MaterialOrders = materialLists
	e.OK(object, "查询成功")
}

func (e SysStudent) Update(c *gin.Context) {
	req := dto.SysStudentUpdateReq{}
	s := service.SysStudent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.Update(&req)
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "成功")
	return
}

func (e SysStudent) ChangeStatus(c *gin.Context) {
	s := service.SysStudent{}
	req := dto.SysStudentById{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.ChangeStatus(&req)
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "成功")
	return

}

//UpdateBuyStatus 批量更新学员购买状态
func (e SysStudent) UpdateBuyStatus(ctx *gin.Context) {
	req := dto.SysStudentUpdateBuyStatusReq{}
	s := service.SysStudent{}
	err := e.MakeContext(ctx).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	if err := s.UpdateBuyStatus(&req); err != nil {
		xlog.Error(err)
		e.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	e.OK(nil, "更新成功")
}

//Excel 导出
func (e SysStudent) Excel(ctx *gin.Context) {
	req := dto.SysStudentExcelReq{}
	s := service.SysStudent{}
	err := e.MakeContext(ctx).
		MakeOrm().
		Bind(&req, binding.Default(ctx.Request.Method, ctx.ContentType())).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	t1, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	t2, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	if t2.Sub(t1) <= 0 {
		e.Error(http.StatusBadRequest, nil, "结束必须大于开始时间")
		return
	}
	lists, err := s.Excel(&req)
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	// 需要导出的第一行文字顺序
	header := []string{"姓名", "电话", "学号", "卡号", "性别", "身份证", "出生日期", "报名日期", "所属年级", "来源学校", "来源渠道", "QQ", "微信", "邮箱", "联系人信息", "备注", "咨询师姓名", "学管师姓名", "文理", "专业类型", "主班型", "叠加课程", "院系", "专业", "学校班级", "考生类别", "原价", "实际缴费金额", "集训天数", "吃住情况", "有无退费", "协议编号", "民族", "家庭住址", "收据编号"} //, "院校", "升本年份", "科别", "线下课程", "级别"}
	f := util.MakeExcel(header, lists)
	ctx.Header("Content-Type", "application/octet-stream")
	ctx.Header("Content-Disposition", "attachment; filename="+"学生列表.xlsx")
	ctx.Header("Content-Transfer-Encoding", "binary")
	f.Write(ctx.Writer)
	// 导出为excel
}

//GetListByClassId 获取某个班级的学生列表
func (e SysStudent) GetListByClassId(ctx *gin.Context) {
	req := dto.GetListByClassIdReq{}
	s := service.SysStudent{}
	err := e.MakeContext(ctx).
		MakeOrm().
		Bind(&req, binding.Default(ctx.Request.Method, ctx.ContentType())).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	lists := make([]models.SysStudent, 0)
	var count int64
	err = s.GetListByClassId(&req, &lists, &count)
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	for i, j := range lists {
		lists[i].ShenBenYear = j.ShenBenTime.Format("2006")
	}
	e.PageOK(lists, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

//GetNoClassStudents 获取没有班级的学生
func (e SysStudent) GetNoClassStudents(ctx *gin.Context) {
	req := dto.GetNoClassStudentListReq{}
	s := service.SysStudent{}
	err := e.MakeContext(ctx).
		MakeOrm().
		Bind(&req, binding.Default(ctx.Request.Method, ctx.ContentType())).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusBadRequest, err, err.Error())
		return
	}
	lists := make([]models.SysStudent, 0)
	var count int64
	err = s.GetNoClassStudents(&req, &lists, &count)
	if err != nil {
		xlog.Error(err)
		e.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	for i, j := range lists {
		lists[i].ShenBenYear = j.ShenBenTime.Format("2006")
	}
	e.PageOK(lists, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
